/*
 * JCaptcha, the open source java framework for captcha definition and integration
 * Copyright (c)  2007 jcaptcha.net. All Rights Reserved.
 * See the LICENSE.txt file distributed with this package.
 */

package org.ponly.cta4j.drawing.text;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import org.ponly.cta4j.drawing.color.ColorGenerator;
import org.ponly.cta4j.drawing.color.SingleColorGenerator;

/**
 * 抽象文本笔刷, 处理文本内容的绘制工作.
 * <p>
 * 子类应该实现 {@link #draw(java.awt.image.BufferedImage, java.text.AttributedString)} 来绘制文本.
 * <br/>
 * 该子类可以通过 acceptedMinLength, acceptedMaxLength 提供对文本内容长度的限制(默认为4~10)
 * </p>
 *
 * @author vacoor
 * @since 1.0
 */
public abstract class AbstractTextBrush implements TextBrush {
    /**
     * 绘制文本的允许最小长度.
     */
    private int min = 4;

    /**
     * 绘制文本的允许最大长度.
     */
    private int max = 10;

    /**
     * 文本的颜色生成器.
     */
    private ColorGenerator colorGenerator = new SingleColorGenerator(Color.BLACK);

    /**
     * 是否为每个字形生成颜色.
     */
    private boolean manageColorPerGlyph;


    /* ************************************************
     *                CONSTRUCTOR
     * ************************************************/

    /**
     * 创建一个默认实例, 默认文本内容长度为 4 ~ 10, 颜色为 黑色.
     */
    public AbstractTextBrush() {
    }

    /**
     * 使用给定文本范围限定.
     *
     * @param acceptedMinLength 允许的最小文本长度
     * @param acceptedMaxLength 允许的最大文本长度
     */
    protected AbstractTextBrush(final int acceptedMinLength, final int acceptedMaxLength) {
        this.min = acceptedMinLength;
        this.max = acceptedMaxLength;
    }

    /**
     * 使用给定文本范围限定和单一文字颜色.
     *
     * @param acceptedMinLength 允许的最小文本长度
     * @param acceptedMaxLength 允许的最大文本长度
     * @param textColor         文本颜色
     */
    protected AbstractTextBrush(final int acceptedMinLength, final int acceptedMaxLength, final Color textColor) {
        this(acceptedMinLength, acceptedMaxLength);

        if (null != textColor) {
            this.colorGenerator = new SingleColorGenerator(textColor);
        }
    }

    /**
     * 使用给定文本范围限定和给定颜色生成器来生成文本颜色.
     *
     * @param acceptedMinLength 允许的最小文本长度
     * @param acceptedMaxLength 允许的最大文本长度
     * @param colorGenerator    文本颜色生成器
     */
    protected AbstractTextBrush(final int acceptedMinLength, final int acceptedMaxLength, final ColorGenerator colorGenerator) {
        this(acceptedMinLength, acceptedMaxLength);
        if (null == colorGenerator) {
            throw new IllegalStateException("ColorGenerator is null");
        }
        this.colorGenerator = colorGenerator;
    }

    /**
     * 使用给定文本范围限定和给定颜色生成器来生成文本颜色, 并设置是否为每个字形生成颜色.
     *
     * @param acceptedMinLength   允许的最小文本长度
     * @param acceptedMaxLength   允许的最大文本长度
     * @param colorGenerator      文本颜色生成器
     * @param manageColorPerGlyph 是否为每个字形生成颜色
     */
    protected AbstractTextBrush(final int acceptedMinLength, final int acceptedMaxLength,
                                final ColorGenerator colorGenerator, final boolean manageColorPerGlyph) {
        this(acceptedMinLength, acceptedMaxLength, colorGenerator);
        this.manageColorPerGlyph = manageColorPerGlyph;
    }

    /* ************************************************
     *                METHODS
     * ************************************************/

    /**
     * 定制 Graphics2D hints.
     *
     * @param g2d Graphics2D
     */
    protected void customizeGraphicsRenderingHints(final Graphics2D g2d) {
        g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    }

    /**
     * @return 获取一个用于文字绘制的颜色
     */
    protected Color getColor() {
        return getColorGenerator().getColor();
    }

    /* *******************************************
     *              GETTER / SETTER
     * ***************************************** */

    /**
     * @return 可以绘制的最大文本长度
     */
    @Override
    public int getMaxTextLength() {
        return max;
    }

    /**
     * @return 可以绘制的最小文本长度
     */
    @Override
    public int getMinTextLength() {
        return min;
    }

    /**
     * @return 当前文本的颜色生成器
     */
    protected ColorGenerator getColorGenerator() {
        return colorGenerator;
    }

    /**
     * 设置文本使用的颜色生成器.
     *
     * @param colorGenerator 颜色生成器
     */
    public void setColorGenerator(final ColorGenerator colorGenerator) {
        this.colorGenerator = colorGenerator;
    }

    /**
     * @return 是否为每个字形生成颜色
     */
    public boolean isManageColorPerGlyph() {
        return manageColorPerGlyph;
    }

    /**
     * 设置是否为每个字形生成颜色.
     *
     * @param manageColorPerGlyph 是否为每个字形生成颜色
     */
    public void setManageColorPerGlyph(final boolean manageColorPerGlyph) {
        this.manageColorPerGlyph = manageColorPerGlyph;
    }
}
